package com.sky.controller.admin;

import com.sky.dto.DataOverViewQueryDTO;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.service.ReportService;
import com.sky.service.UserService;
import com.sky.service.impl.ReportServiceImpl;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.core.Local;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/admin/report")
@Slf4j
@Api(tags = "统计分析相关接口")
public class ReportController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private ReportService reportService;
    @Autowired
    private UserService userService;

    /**
     * 营业额统计接口
     *
     * @param begin 开始日期
     * @param end   结束日期
     * @return R
     */
    @GetMapping("/turnoverStatistics")
    public Result<TurnoverReportVO> turnoverStatistics(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        TurnoverReportVO vo = orderService.getTurnoverStatistics(begin, end);
        return Result.success(vo);
    }

    /**
     * 用户人数统计接口
     *
     * @param begin 开始日期
     * @param end   结束日期
     * @return R
     */
    @GetMapping("/userStatistics")
    public Result<UserReportVO> userStatistics(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                               @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {

        UserReportVO vo = userService.getUserStatistics(begin, end);
        return Result.success(vo);
    }

    /**
     * 订单统计接口
     *
     * @param dto dto
     * @return R
     */
    @GetMapping("/ordersStatistics")
    public Result<OrderReportVO> ordersStatistics(DataOverViewQueryDTO dto) {
        OrderReportVO vo = orderService.getOrdersStatistics(dto);
        return Result.success(vo);
    }

    /**
     * 商品(菜品,套餐)销量排名前10统计
     *
     * @param dto dto
     * @return R
     */
    @GetMapping("/top10")
    public Result<SalesTop10ReportVO> top10(DataOverViewQueryDTO dto) {
        SalesTop10ReportVO vo = orderService.getTop10(dto);
        return Result.success(vo);
    }


    /**
     * 导出Excel报表 统计最近30天的数据，并导出报表
     * @param response 响应
     * @return R
     */
    @GetMapping("/export")
    public Result<String> export(HttpServletResponse response) {
        reportService.businessDataExport(response);
        return Result.success();
    }
}
